package com.gable.report.service.impl;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.dao.IChartsForAreaMaintenanceDao;
import com.gable.report.model.ChartsAreaQuery;
import com.gable.report.model.ChartsForMaintenance;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForAreaMaintenanceService;
import com.gable.report.thread.RedisListStoreThread;
import com.gable.report.thread.RedisValueStoreThread;
import com.gable.report.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gable.report.util.ChartsUtil.getParamMap;
import static com.gable.report.util.ChartsUtil.isQueryNullOrOnlyType;
import static com.gable.report.util.RedisUtil.getAreaModuleKey;

/**
 * Created by mj on 2017/7/27.
 */
@Service
public class ChartsForAreaMaintenanceServiceImpl implements ChartsForAreaMaintenanceService {

    @Autowired
    IChartsForAreaMaintenanceDao chartsForAreaMaintenanceDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ReturnResultUtil queryHospitalAssetsForMaintenance(User user,ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<ChartsForMaintenance> list;
            if(chartsAreaQuery.getMaintenanceType() == null){
                return new ReturnResultUtil(0,"参数错误");
            }
            if(RedisUtil.isOn && isQueryNullOrOnlyType(chartsAreaQuery)){
                String key = getAreaModuleKey("maintenance",user,"hospital",chartsAreaQuery);
                List redisList = null;
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        key = key +"_tag_1";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryHospitalForUpkeep(paramMap);
                        break;
                    case 2:
                        key = key +"_tag_2";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryHospitalForInspection(paramMap);
                        break;
                    case 3:
                        key = key +"_tag_3";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryHospitalForCompulsory(paramMap);
                        break;
                    default:
                        list = null;

                }
                if(redisList == null && list != null){
                    // 把数据存入缓存
                    new Thread(new RedisListStoreThread(redisTemplate, key, list)).start();
                }
            }else{
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        list = chartsForAreaMaintenanceDao.queryHospitalForUpkeep(paramMap);
                        break;
                    case 2:
                        list = chartsForAreaMaintenanceDao.queryHospitalForInspection(paramMap);
                        break;
                    case 3:
                        list = chartsForAreaMaintenanceDao.queryHospitalForCompulsory(paramMap);
                        break;
                    default:
                        list = null;
                }

            }
            if(!CollectionUtils.isEmpty(list)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("list",list);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryDeviceTypeSubAssetsForMaintenance(User user,ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<ChartsForMaintenance> list;
            if(chartsAreaQuery.getMaintenanceType() == null){
                return new ReturnResultUtil(0,"参数错误");
            }
            if(RedisUtil.isOn && isQueryNullOrOnlyType(chartsAreaQuery)){
                String key = getAreaModuleKey("maintenance",user,"type",chartsAreaQuery);
                List redisList = null;
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        key = key +"_tag_1";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryDeviceTypeForUpkeep(paramMap);
                        break;
                    case 2:
                        key = key +"_tag_2";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryDeviceTypeForInspection(paramMap);
                        break;
                    case 3:
                        key = key +"_tag_3";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryDeviceTypeForCompulsory(paramMap);
                        break;
                    default:
                        list = null;

                }
                if(redisList == null && list != null){
                    // 把数据存入缓存
                    new Thread(new RedisListStoreThread(redisTemplate, key, list)).start();
                }
            }else{
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        list = chartsForAreaMaintenanceDao.queryDeviceTypeForUpkeep(paramMap);
                        break;
                    case 2:
                        list = chartsForAreaMaintenanceDao.queryDeviceTypeForInspection(paramMap);
                        break;
                    case 3:
                        list = chartsForAreaMaintenanceDao.queryDeviceTypeForCompulsory(paramMap);
                        break;
                    default:
                        list = null;
                }

            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",list);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryManufactureAssetsForMaintenance(User user,ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<ChartsForMaintenance> list;
            if(chartsAreaQuery.getMaintenanceType() == null){
                return new ReturnResultUtil(0,"参数错误");
            }
            if(RedisUtil.isOn && isQueryNullOrOnlyType(chartsAreaQuery)){
                String key = getAreaModuleKey("maintenance",user,"brand",chartsAreaQuery);
                List redisList = null;
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        key = key +"_tag_1";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryManufactureForUpkeep(paramMap);
                        break;
                    case 2:
                        key = key +"_tag_2";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryManufactureForInspection(paramMap);
                        break;
                    case 3:
                        key = key +"_tag_3";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryManufactureForCompulsory(paramMap);
                        break;
                    default:
                        list = null;

                }
                if(redisList == null && list != null){
                    // 把数据存入缓存
                    new Thread(new RedisListStoreThread(redisTemplate, key, list)).start();
                }
            }else{
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        list = chartsForAreaMaintenanceDao.queryManufactureForUpkeep(paramMap);
                        break;
                    case 2:
                        list = chartsForAreaMaintenanceDao.queryManufactureForInspection(paramMap);
                        break;
                    case 3:
                        list = chartsForAreaMaintenanceDao.queryManufactureForCompulsory(paramMap);
                        break;
                    default:
                        list = null;
                }

            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",list);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryModelAssetsForMaintenance(User user,ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<ChartsForMaintenance> list;
            if(RedisUtil.isOn && isQueryNullOrOnlyType(chartsAreaQuery)){
                String key = getAreaModuleKey("maintenance",user,"model",chartsAreaQuery);
                List redisList = null;
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        key = key +"_tag_1";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryModelForUpkeep(paramMap);
                        break;
                    case 2:
                        key = key +"_tag_2";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryModelForInspection(paramMap);
                        break;
                    case 3:
                        key = key +"_tag_3";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryModelForCompulsory(paramMap);
                        break;
                    default:
                        list = null;

                }
                if(redisList == null && list != null){
                    // 把数据存入缓存
                    new Thread(new RedisListStoreThread(redisTemplate, key, list)).start();
                }
            }else{
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        list = chartsForAreaMaintenanceDao.queryModelForUpkeep(paramMap);
                        break;
                    case 2:
                        list = chartsForAreaMaintenanceDao.queryModelForInspection(paramMap);
                        break;
                    case 3:
                        list = chartsForAreaMaintenanceDao.queryModelForCompulsory(paramMap);
                        break;
                    default:
                        list = null;
                }

            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",list);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryAssets(User user,ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<ChartsForMaintenance> list;
            if(RedisUtil.isOn && isQueryNullOrOnlyType(chartsAreaQuery)){
                String key = getAreaModuleKey("maintenance",user,"assets",chartsAreaQuery);
                List redisList = null;
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        key = key +"_tag_1";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryAssetsForUpkeep(paramMap);
                        break;
                    case 2:
                        key = key +"_tag_2";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryAssetsForInspection(paramMap);
                        break;
                    case 3:
                        key = key +"_tag_3";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryAssetsForCompulsory(paramMap);
                        break;
                    default:
                        list = null;

                }
                if(redisList == null && list != null){
                    // 把数据存入缓存
                    new Thread(new RedisListStoreThread(redisTemplate, key, list)).start();
                }
            }else{
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        list = chartsForAreaMaintenanceDao.queryAssetsForUpkeep(paramMap);
                        break;
                    case 2:
                        list = chartsForAreaMaintenanceDao.queryAssetsForInspection(paramMap);
                        break;
                    case 3:
                        list = chartsForAreaMaintenanceDao.queryAssetsForCompulsory(paramMap);
                        break;
                    default:
                        list = null;
                }

            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",list);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryOverview(User user, ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            Map<String, Object> inspectionOverview;
            Map<String, Object> upkeepOverview;
            Map<String, Object> compulsoryOverview;
            if(RedisUtil.isOn && isQueryNullOrOnlyType(chartsAreaQuery)) {
                String key1 = getAreaModuleKey("maintenance", user, "overview_upkeep", chartsAreaQuery);
                String key2 = getAreaModuleKey("maintenance", user, "overview_inspection", chartsAreaQuery);
                String key3 = getAreaModuleKey("maintenance", user, "overview_compulsory", chartsAreaQuery);
                upkeepOverview = RedisUtil.getValue(redisTemplate, key1);
                inspectionOverview = RedisUtil.getValue(redisTemplate, key2);
                compulsoryOverview = RedisUtil.getValue(redisTemplate, key3);
                if(upkeepOverview == null){
                    upkeepOverview = chartsForAreaMaintenanceDao.queryOverviewForUpkeep(paramMap);
                    inspectionOverview = chartsForAreaMaintenanceDao.queryOverviewForInspection(paramMap);
                    compulsoryOverview = chartsForAreaMaintenanceDao.queryOverviewForCompulsory(paramMap);
                    new Thread(new RedisValueStoreThread(redisTemplate, key1, upkeepOverview)).start();
                    new Thread(new RedisValueStoreThread(redisTemplate, key2, inspectionOverview)).start();
                    new Thread(new RedisValueStoreThread(redisTemplate, key3, compulsoryOverview)).start();
                }
            }else{
                upkeepOverview = chartsForAreaMaintenanceDao.queryOverviewForUpkeep(paramMap);
                inspectionOverview = chartsForAreaMaintenanceDao.queryOverviewForInspection(paramMap);
                compulsoryOverview = chartsForAreaMaintenanceDao.queryOverviewForCompulsory(paramMap);
            }
            Map<String,Object> returnMap = new HashMap<String,Object>();
            returnMap.put("inspectionOverview",inspectionOverview);
            returnMap.put("upkeepOverview",upkeepOverview);
            returnMap.put("compulsoryOverview",compulsoryOverview);
            return new ReturnResultUtil(1,"success",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryForMonth(User user,ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<ChartsForMaintenance> list;
            if(RedisUtil.isOn && isQueryNullOrOnlyType(chartsAreaQuery)){
                String key = getAreaModuleKey("maintenance",user,"month",chartsAreaQuery);
                List redisList = null;
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        key = key +"_tag_1";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryForMonthWithUpkeep(paramMap);
                        break;
                    case 2:
                        key = key +"_tag_2";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryForMonthWithInspection(paramMap);
                        break;
                    case 3:
                        key = key +"_tag_3";
                        redisList = RedisUtil.getListValue(redisTemplate, key);
                        list = redisList != null ? redisList : chartsForAreaMaintenanceDao.queryForMonthWithCompulsory(paramMap);
                        break;
                    default:
                        list = null;

                }
                if(redisList == null && list != null){
                    // 把数据存入缓存
                    new Thread(new RedisListStoreThread(redisTemplate, key, list)).start();
                }
            }else{
                switch (chartsAreaQuery.getMaintenanceType()){
                    case 1:
                        list = chartsForAreaMaintenanceDao.queryForMonthWithUpkeep(paramMap);
                        break;
                    case 2:
                        list = chartsForAreaMaintenanceDao.queryForMonthWithInspection(paramMap);
                        break;
                    case 3:
                        list = chartsForAreaMaintenanceDao.queryForMonthWithCompulsory(paramMap);
                        break;
                    default:
                        list = null;
                }

            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",list);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryActualRecords(User user, ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<Map<String,Object>> chartsForMaintenances;
            if(chartsAreaQuery.getMaintenanceType() == null){
                return new ReturnResultUtil(0,"参数错误");
            }
            if(chartsAreaQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryActualRecordsForUpkeep(paramMap);
            }else if(chartsAreaQuery.getMaintenanceType() == 2){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryActualRecordsForInspection(paramMap);
            }else{
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryActualRecordsForCompulsory(paramMap);
            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",chartsForMaintenances);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryPlanRecords(User user, ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<Map<String,Object>> chartsForMaintenances;
            if(chartsAreaQuery.getMaintenanceType() == null){
                return new ReturnResultUtil(0,"参数错误");
            }
            if(chartsAreaQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryPlanRecordsForUpkeep(paramMap);
            }else if(chartsAreaQuery.getMaintenanceType() == 2){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryPlanRecordsForInspection(paramMap);
            }else{
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryPlanRecordsForCompulsory(paramMap);
            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",chartsForMaintenances);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryActualAssets(User user, ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<Map<String,Object>> chartsForMaintenances;
            if(chartsAreaQuery.getMaintenanceType() == null){
                return new ReturnResultUtil(0,"参数错误");
            }
            if(chartsAreaQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryActualAssetsForUpkeep(paramMap);
            }else if(chartsAreaQuery.getMaintenanceType() == 2){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryActualAssetsForInspection(paramMap);
            }else{
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryActualAssetsForCompulsory(paramMap);
            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",chartsForMaintenances);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryPlanAssets(User user, ChartsAreaQuery chartsAreaQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
            List<Map<String,Object>> chartsForMaintenances;
            if(chartsAreaQuery.getMaintenanceType() == null){
                return new ReturnResultUtil(0,"参数错误");
            }
            if(chartsAreaQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryPlanAssetsForUpkeep(paramMap);
            }else if(chartsAreaQuery.getMaintenanceType() == 2){
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryPlanAssetsForInspection(paramMap);
            }else{
                chartsForMaintenances = chartsForAreaMaintenanceDao.queryPlanAssetsForCompulsory(paramMap);
            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list",chartsForMaintenances);
            return new ReturnResultUtil(1,"success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }


}
